package com.opgea.token.web.dto;

import java.io.Serializable;

import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;

import com.opgea.token.domain.entities.Login;
import com.opgea.token.domain.interfaces.Marshallable;
import com.opgea.token.domain.qualifiers.UserTypeQualifier;

public class LoginDTO implements Serializable, Marshallable<Login> {

    private static final long serialVersionUID = -6345676861925882237L;
    private long id;

    @NotEmpty(message = "LoginId cann\'t be empty.")
    @Size(max = 40, min = 6, message = "LoginId input length incorrect.")
    private String loginId;

    @NotEmpty(message = "Password cann\'t be empty.")
    private String password;

    @NotEmpty(message = "Confirm Password cann\'t be empty.")
    private String confirmPassword;

    @NotEmpty
    private String userType;
    private long employeeId;
    private long companyId;
    private String companyName;
    private long destinationId;
    private String destinationName;

    public LoginDTO() {
    }

    public long getId() {
	return id;
    }

    public void setId(long id) {
	this.id = id;
    }

    public String getLoginId() {
	return loginId;
    }

    public void setLoginId(String loginId) {
	this.loginId = loginId;
    }

    public String getPassword() {
	return password;
    }

    public void setPassword(String password) {
	this.password = password;
    }

    public String getConfirmPassword() {
	return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
	this.confirmPassword = confirmPassword;
    }

    public String getUserType() {
	return userType;
    }

    public void setUserType(String userType) {
	this.userType = userType;
    }

    public long getEmployeeId() {
	return employeeId;
    }

    public void setEmployeeId(long employeeId) {
	this.employeeId = employeeId;
    }

    public long getCompanyId() {
	return companyId;
    }

    public void setCompanyId(long companyId) {
	this.companyId = companyId;
    }

    public String getCompanyName() {
	return companyName;
    }

    public void setCompanyName(String companyName) {
	this.companyName = companyName;
    }

    public long getDestinationId() {
	return destinationId;
    }

    public void setDestinationId(long destinationId) {
	this.destinationId = destinationId;
    }

    public String getDestinationName() {
	return destinationName;
    }

    public void setDestinationName(String destinationName) {
	this.destinationName = destinationName;
    }

    /**
     * Copy values from Login entity to LoginDTO.
     * 
     * @param login
     */
    public void marshall(Login login) {
	this.setId(login.getId());
	this.setLoginId(login.getLoginId());
	this.setPassword(login.getPassword());
	this.setConfirmPassword(login.getPassword());
	if (login.getUserType() != null) {
	    System.out.println("User type: " + login.getUserType().toString());
	    this.setUserType(login.getUserType().toString());
	}
	if (login.getEmployee() != null) {
	    this.employeeId = login.getEmployee().getId();
	}
	if (login.getEmployee() != null
		&& login.getEmployee().getCompany() != null) {
	    this.companyId = login.getEmployee().getCompany().getId();
	    this.companyName = login.getEmployee().getCompany().getName();
	}
	if (login.getEmployee() != null
		&& login.getEmployee().getDestination() != null) {
	    this.destinationId = login.getEmployee().getDestination().getId();
	    this.destinationName = login.getEmployee().getDestination()
		    .getName();
	}
    }

    /**
     * Copy values from LoginDTO to Login entity.
     * 
     * @param login
     * @return
     */
    public Login unmarshall(Login login) {
	login.setId(this.id);
	login.setLoginId(this.loginId);
	login.setPassword(this.password);
	if (this.userType != null) {
	    login.setUserType(UserTypeQualifier.valueOf(this.userType));
	}
	return login;
    }

    @Override
    public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + (int) (id ^ (id >>> 32));
	return result;
    }

    @Override
    public boolean equals(Object obj) {
	if (this == obj)
	    return true;
	if (obj == null)
	    return false;
	if (getClass() != obj.getClass())
	    return false;
	LoginDTO other = (LoginDTO) obj;
	if (id != other.id)
	    return false;
	return true;
    }

    @Override
    public String toString() {
	return "LoginDTO [id=" + id + ", loginId=" + loginId + ", password="
		+ password + ", confirmPassword=" + confirmPassword
		+ ", userType=" + userType + "]";
    }

}
